{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,\"hello\",True]\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 访问列表中的值\n",
    "* 与字符串的索引一样，列表索引从0开始。列表可以进行截取、组合等。\n",
    "* 使用下标索引来访问列表中的值，同样你也可以使用方括号的形式截取字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True] 1 [1, 2, 'hello']\n"
     ]
    }
   ],
   "source": [
    "print(list1,list1[0],list1[0:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 更新列表\n",
    "* 你可以对列表的数据项进行修改或更新，你也可以使用append()方法来添加列表项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[111, 2, 'hello', True]\n",
      "[111, 2, 'hello', True]\n"
     ]
    }
   ],
   "source": [
    "print(list1)\n",
    "list1[0]=111\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 删除列表元素\n",
    "* 可以使用 del 语句来删除列表的的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 'hello', True]\n",
      "['hello', True]\n"
     ]
    }
   ],
   "source": [
    "print(list1)\n",
    "del list1[0]\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表脚本操作符\n",
    "列表对 + 和 \\* 的操作符与字符串相似。+ 号用于组合列表，* 号用于重复列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True]\n",
      "4\n",
      "[1, 2, 'hello', True, 1, 2, 'hello', True]\n",
      "[1, 2, 'hello', True, 1, 2, 'hello', True, 1, 2, 'hello', True]\n",
      "True\n",
      "1\n",
      "2\n",
      "hello\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,\"hello\",True]\n",
    "print(list1) # 打印list1\n",
    "print(len(list1)) # 求list1列表的长度\n",
    "list2=list1+list1 # 两个list拼接\n",
    "print(list2)\n",
    "list3=list1*3\n",
    "print(list3)\n",
    "print(1 in list1)\n",
    "for x in list1:print(x,end='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表截取与拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True]\n",
      "1\n",
      "True\n",
      "[1, 2, 'hello']\n"
     ]
    }
   ],
   "source": [
    "print(list1)\n",
    "print(list1[0])\n",
    "print(list1[-1])\n",
    "print(list1[:-1]) # [)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True, 1, 2, 'hello', True]\n"
     ]
    }
   ],
   "source": [
    "print(list1+list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 嵌套列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True]\n",
      "[[1, 2, 'hello', True], [1, 2, 'hello', True], [1, 2, 'hello', True, 1, 2, 'hello', True]]\n"
     ]
    }
   ],
   "source": [
    "print(list1)\n",
    "list2=[list1,list1,list1*2]\n",
    "print(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'hello', True]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(list2[0])\n",
    "print(list2[0][1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表函数和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "10\n",
      "9\n",
      "0\n",
      "<class 'tuple'> <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "list1=range(10)\n",
    "print(list1)\n",
    "print(len(list1))\n",
    "print(max(list1))\n",
    "print(min(list1))\n",
    "tuple1=(1,2,3)\n",
    "print(type(tuple1),type(list(tuple1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 2, 3, 4, 'world']\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,3,4]\n",
    "# append 在列表末尾添加新的对象\n",
    "print(list1)\n",
    "list1.append(\"world\")\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# count 统计某个元素在列表中出现的次数\n",
    "print(list1.count(1))\n",
    "xa=[0,False]\n",
    "print(1==True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "# extend 在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）\n",
    "list1=[1,2,3,4]\n",
    "list2=[4,5,6]\n",
    "list1.extend(list2)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 4, 5, 6]\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# index  从列表中找出某个值第一个匹配项的索引位置\n",
    "print(list1)\n",
    "print(list1.index(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 10, 1, 2, 3, 4, 4, 5, 6]\n",
      "[10, 10, 10, 1, 2, 3, 4, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "# insert 将对象插入列表\n",
    "print(list1)\n",
    "list1.insert(0,10)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 111, 111, 10, 10, 1, 2, 3, 4, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "list1.insert(1,111)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 111, 111, 10, 10, 1, 2, 3, 4, 4, 5]\n",
      "5\n",
      "[10, 111, 111, 10, 10, 1, 2, 3, 4, 4]\n"
     ]
    }
   ],
   "source": [
    "# pop 移除列表中的一个元素（默认最后一个元素），并且返回该元素的值\n",
    "print(list1)\n",
    "print(list1.pop())\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111\n",
      "[111, 10, 10, 1, 2, 3, 4, 4]\n"
     ]
    }
   ],
   "source": [
    "print(list1.pop(0))\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[111, 10, 1, 2, 3, 4, 4]\n",
      "[111, 1, 2, 3, 4, 4]\n"
     ]
    }
   ],
   "source": [
    "# remove 移除列表中某个值的第一个匹配项\n",
    "print(list1)\n",
    "list1.remove(10)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 4, 3, 2, 1, 111]\n",
      "None\n",
      "[111, 1, 2, 3, 4, 4]\n"
     ]
    }
   ],
   "source": [
    "# reverse 反向列表中元素\n",
    "print(list1)\n",
    "print(list1.reverse())\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[111, 2, 2]\n",
      "[2, 2, 111]\n"
     ]
    }
   ],
   "source": [
    "# list.sort(cmp=None, key=None, reverse=False) 对原列表进行排序\n",
    "print(list1)\n",
    "list1.sort(reverse=False)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[111, 4, 4, 3, 2, 1]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# clear 清空列表\n",
    "print(list1)\n",
    "list1.clear()\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 2]\n",
      "[1, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "# copy 复制列表\n",
    "list1=[1,2,2]\n",
    "print(list1)\n",
    "list2=list1.copy()\n",
    "print(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 2]\n",
      "[1, 2, 2] [111, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,2]\n",
    "print(list1)\n",
    "list2=list1.copy()\n",
    "list2[0]=111\n",
    "print(list1,list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 2]\n",
      "[111, 2, 2] [111, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,2]\n",
    "print(list1)\n",
    "list2=list1\n",
    "list2[0]=111\n",
    "print(list1,list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
    }
   ],
   "source": [
    "n_list = [x * 2 for x in range(10)]\n",
    "print(n_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "[4]\n"
    }
   ],
   "source": [
    "new_list = [x * 2 for x in range(10) if x == 2]\n",
    "print(new_list)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit ('base': conda)",
   "language": "python",
   "name": "python37364bitbaseconda6fac67c13ee249de96fe454f62091838"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}